
package com.jf.cloud.product.controller.multishop;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.product.vo.GiveawayVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.dto.GiveawayDTO;
import com.jf.cloud.product.service.GiveawayService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 赠品表
 *
 * @author zz
 * @date 2022-08-16 11:11:58
 */
@RestController("multishopGiveawayController")
@RequestMapping("/m/giveaway")
@Tag(name = "赠品表")
public class GiveawayController {

    @Autowired
    private GiveawayService giveawayService;

    @GetMapping("/page")
    @Operation(summary = "获取赠品表列表" , description = "分页获取赠品表列表")
    public ServerResponseEntity<PageVO<GiveawayVO>> page(@Valid PageDTO pageDTO, @ParameterObject GiveawayDTO giveawayDTO) {
        giveawayDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<GiveawayVO> giveawayPage = giveawayService.listByShopId(pageDTO, giveawayDTO);
        return ServerResponseEntity.success(giveawayPage);
    }

    @GetMapping
    @Operation(summary = "获取赠品表" , description = "根据giveawayId获取赠品表")
    public ServerResponseEntity<GiveawayVO> getByGiveawayId(@RequestParam Long giveawayId) {
        Long shopId = AuthUserContext.get().getTenantId();
        GiveawayVO giveawayVO = giveawayService.getByGiveawayId(giveawayId);
        if (!Objects.equals(shopId, giveawayVO.getShopId())) {
            throw new LuckException("该赠品信息不属于你的店铺");
        }
        return ServerResponseEntity.success(giveawayVO);
    }

    @PostMapping
    @Operation(summary = "保存赠品表" , description = "保存赠品表")
    public ServerResponseEntity<Void> save(@Valid @RequestBody GiveawayDTO giveawayDTO) {
        giveawayDTO.setShopId(AuthUserContext.get().getTenantId());
        giveawayDTO.setGiveawayId(null);
        giveawayService.save(giveawayDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新赠品表" , description = "更新赠品表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody GiveawayDTO giveawayDTO) {
        giveawayDTO.setShopId(AuthUserContext.get().getTenantId());
        giveawayService.update(giveawayDTO);
        return ServerResponseEntity.success();
    }


    @PutMapping("/changeStatus")
    @Operation(summary = "修改赠品状态" , description = "修改赠品状态")
    public ServerResponseEntity<Void> changeStatus(@RequestParam(value = "status") Integer status, @RequestParam(value = "giveawayId") Long giveawayId) {
        giveawayService.changeStatus(giveawayId, status);
        return ServerResponseEntity.success();
    }


}
